#ifndef AVALON_DELIMITEDREADER_HPP
#define AVALON_DELIMITEDREADER_HPP
#include "Avalon/Base/Handle.hpp"
#include "Avalon/IO/Reader.hpp"

namespace Avalon {
namespace IO {

  /*! \class DelimitedReader
   *  \brief Reads character delimited data.
   *  \author Kamal Mansouri <kranar@hotmail.com>
   */
  class DelimitedReader : public Reader {
    public:

      //! Constructs a DelimitedReader.
      /*!
        \param source The source to read from.
        \param delimiter The delimiter.
      */
      DelimitedReader(Carry<Reader> source, char delimiter);

      virtual ~DelimitedReader();

      virtual bool IsAtEndOfDataSource();

      virtual Threading::Async<int>::Ptr Read(void* destination, int size);

      virtual Threading::Async<int>::Ptr Read(Buffer* destination, int size);

      using Reader::Read;

    private:
      boost::recursive_mutex m_mutex;
      Handle<Reader> m_source;
      char m_delimiter;
      char m_nextCharacter;
      Threading::Async<int>::Ptr m_sourceRead;

      void OnRead(Buffer* buffer, void* destination, int sizeToRead,
        int sizeRead, const Threading::AsyncOwner<int>::Ptr& delimitedRead,
        const Threading::Async<int>::Ptr& sourceRead);
  };
}
}

#endif // AVALON_DELIMITEDREADER_HPP
